<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class BroadcastTestEvent extends Event implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $uid;
    public $broadcastQueue = 'broadcast';
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($uid)
    {
        //
        $this->uid = $uid;
    }

    /**
     * Get the channels the event should broadcast on.获取事件广播的频道.(广播到哪个频道)
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        //return ['test-channel'];
        /** broadcastOn。该方法应该返回一个事件广播频道或频道数组。这些频道必须是Channel、PrivateChannel或PresenceChannel的实例，Channel频道表示任意用户可以订阅公共频道，而PrivateChannels或PresenceChannels则代表需要进行频道授权的私有频道 */
        return new PrivateChannel('order.'.$this->uid);
    }

    /**
     * 明确的指出要广播什么数据
     *
     * 默认情况下，Event中的所有public属性都会被序列化后广播。上面的例子中就是$user_id这个属性。你也可以使用broadcastWith这个方法，明确的指出要广播什么数据。例如：
     */
    public function broadcastWith(){
        return ['uid' => $this->uid];
    }

    /**
     * 默认情况下，Laravel 会使用事件的类名来广播事件，不过，你可以通过在事件中定义broadcastAs方法来自定义广播名称：
     */
    public function broadcastAs()
    {
        return 'server.created';
    }
}
